package com.demo.connectdbtest.module.ssh.controller;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.util.FS;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@Slf4j
public class GitUtil {
    private static String keyPath = "C:\\Users\\Administrator\\.ssh\\id_rsa.pub"; //私钥文件
    private static String localCodeDir = "C:\\Users\\Administrator\\Desktop\\practice\\demo"; //本地文件夹
    private static String remoteRepoPath = "ssh://git@10.252.171.20:21022/SQD/testproject.git"; //git地址
    private static SshSessionFactory sshSessionFactory;

    static {
        sshSessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host host, Session session) {
                session.setConfig("StrictHostKeyChecking", "no");
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch sch = super.createDefaultJSch(fs);
                sch.addIdentity(keyPath); //添加私钥文件
                return sch;
            }
        };
    }

    //克隆代码,一般只执行一次！branch:分支
    public void clone(String branch) throws Exception {

        Git git = Git.cloneRepository().setURI(remoteRepoPath) //设置远程URI
                .setBranch(branch)   //设置clone下来的分支,默认master
                .setDirectory(new File(localCodeDir))
                .setTransportConfigCallback(transport -> {
                    SshTransport sshTransport = (SshTransport) transport;
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                })//设置下载存放路径
                .call();
        git.close();
    }


    //pull远程git的代码
  /*  public boolean pull() {
        boolean pullFlag = true;
        try (Git git = Git.open(new File(localCodeDir))) {
            git.pull().setTransportConfigCallback(
                    transport -> {
                        SshTransport sshTransport = (SshTransport) transport;
                        sshTransport.setSshSessionFactory(sshSessionFactory);
                    }
            ).call();
        } catch (Exception e) {
            e.printStackTrace();
            pullFlag = false;
        }
        return pullFlag;
    }

    //add/commit/push
    //fileName:文件名。如果有上级目录那则是path/filename
    //desc:commit的描述
    //content:filename中的内容,可以根据需求添加逻辑
    public boolean commitAndPush(String fileName, String desc, String content) {
        //先先进行pull
        boolean pull = this.pull();
        if (pull) {
            System.out.println(String.format("Pull successful!! filePath:%s,desc:%s", fileName, desc));
        }

        boolean commitAndPushFlag = true;
        try (Git git = Git.open(new File(localCodeDir))) {
            FileUtils.writeStringToFile(
                    new File(localCodeDir, fileName),
                    content,
                    "UTF-8"
            );
            git.add().addFilepattern(fileName).call();
            //提交
            git.commit().setMessage(desc).call();
            //推送到远程
            git.push().setTransportConfigCallback(
                    transport -> {
                        SshTransport sshTransport = (SshTransport) transport;
                        sshTransport.setSshSessionFactory(sshSessionFactory);
                    }
            ).call();
            System.out.println("Commit And Push file " + fileName + " to repository at " + git.getRepository().getDirectory());
        } catch (Exception e) {
            e.printStackTrace();
            commitAndPushFlag = false;
            System.out.println("Commit And Push error! \n" + e.getMessage());
        }
        return commitAndPushFlag;
    }*/


}